<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let p1 = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve('1111');
            },2000)
        })
        let p2 = new Promise((resolve, reject) => {
            setTimeout(() => {
                // resolve('2222');
                reject('222')
            },1000)
        })
        let p3 = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve('3333');
            },3000)
        })
        Promise.allSettled([p1, p2, p3]).then(res => {
            console.log(res);
        },err => {
            console.log(err, '原生');
        })
        // all方法的实现
        let arr = [p1, p2, p3];
        let newArr = [];
        // let obj = {};
        let count = 0;
        function myAllSettled(arr) {
            return new Promise((resolve, reject) =>{
                arr.forEach( (p, index) => {
                    p.then(res => {
                        let obj = {
                            status: 'fulfilled',
                            value: res
                        }
                        newArr[index] = obj;
                        count++;
                        if (count >= arr.length) {
                            resolve(newArr)
                        }
                    },err => {
                        let obj = {
                            reason : 'rejected',
                            value: err
                        }
                        newArr[index] = obj;
                        count++;
                        if (count >= arr.length) {
                            resolve(newArr)
                        }
                    })
                })

            })
        }
        myAllSettled(arr).then(res => {
            console.log(res ,'own');
        })
    </script>
</body>
</html>